package cn.hamster3.mc.plugin.core.bukkit.impl;

import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils;
import com.google.gson.*;
import org.bukkit.potion.PotionEffect;

import java.lang.reflect.Type;

public final class PotionEffectAdapter implements JsonSerializer<PotionEffect>, JsonDeserializer<PotionEffect> {
    public static final PotionEffectAdapter INSTANCE = new PotionEffectAdapter();

    private PotionEffectAdapter() {
    }

    @Override
    public PotionEffect deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        if (json.isJsonNull()) {
            return null;
        }
        return CoreBukkitUtils.deserializePotionEffect(json.getAsJsonObject());
    }

    @Override
    public JsonElement serialize(PotionEffect src, Type typeOfSrc, JsonSerializationContext context) {
        if (src == null) {
            return JsonNull.INSTANCE;
        }
        return CoreBukkitUtils.serializePotionEffect(src);
    }
}
